package com.senjoeson.abilityservice.action;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.content.Context;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.senjoeson.abilityservice.service.AutoAccessibilityService;

/**
 * @USER senjoeson
 * @DATE 2021/1/30 7:25 下午
 * @Desc
 **/
public class UserSwipeActionImpl extends ZeroActionContext implements UserSwipeAction {


    public UserSwipeActionImpl(AutoAccessibilityService autoAccessibilityService) {
        super(autoAccessibilityService);
        init();
    }

    int mStrokeLenPxX;  // Gesture stroke size, in pixels
    int mStrokeLenPxY;
    Point mCenter;  // Center of screen. Gestures all start from this point.
    static final float GESTURE_LENGTH_INCHES = 1.0f;
    static final long STROKE_MS = 400;
    static final long GESTURE_DISPATCH_TIMEOUT_MS = 3000;
    static final long GESTURE_RECOGNIZE_TIMEOUT_MS = 3000;
    boolean mHasTouchScreen;
    boolean mScreenBigEnough;

    private void init() {

        WindowManager windowManager = (WindowManager) autoAccessibilityService.getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        final DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getRealMetrics(metrics);
        mStrokeLenPxX = (int) (GESTURE_LENGTH_INCHES * metrics.xdpi);
        mStrokeLenPxY = (int) (GESTURE_LENGTH_INCHES * metrics.ydpi);
        mCenter = new Point((int) metrics.widthPixels / 2, (int) metrics.heightPixels / 2);
        mStrokeLenPxX = (int) (GESTURE_LENGTH_INCHES * metrics.xdpi);
        mStrokeLenPxY = (int) (GESTURE_LENGTH_INCHES * metrics.ydpi);
        mScreenBigEnough = (metrics.widthPixels / (2 * metrics.xdpi) > GESTURE_LENGTH_INCHES)
                && (metrics.heightPixels / (2 * metrics.ydpi) > GESTURE_LENGTH_INCHES);

    }

    private static final String TAG = "UserActionImpl";

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public boolean swipeLeft() {
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Point point = new Point(-mStrokeLenPxX, mCenter.y);
        // mCenter.offset(mCenter.x*2,0);

//        Path path = new Path();
////        path.moveTo(mCenter.x * 2, mCenter.y);//先移动到右侧的边线中间.
////        //然后向左移动
////        path.lineTo(0, mCenter.y);
//        path.moveTo(1080, 1920>>1);
//        path.lineTo(0, 1920>>1);
        Path path = linePath(mCenter, point, null);
        Log.d(TAG, "swipeLeft: center===>" + mCenter.toString() + ", other point===>" + point.toString()+",path===>"+path.toString());
        GestureDescription gestureDescription = builder
                .addStroke(new GestureDescription.StrokeDescription(path, 0, 1000, false))
                .build();

        autoAccessibilityService.runOnServiceSync(() -> {
            autoAccessibilityService.dispatchGesture(gestureDescription, new AccessibilityService.GestureResultCallback() {
                @Override
                public void onCompleted(GestureDescription gestureDescription) {
                    super.onCompleted(gestureDescription);

                }

                @Override
                public void onCancelled(GestureDescription gestureDescription) {
                    super.onCancelled(gestureDescription);
                }
            }, autoAccessibilityService.getHandler());
        });
        return false;
    }

    Path linePath(Point startPoint, Point delta1, @Nullable Point delta2) {
        Path path = new Path();
        path.moveTo(startPoint.x-300, startPoint.y);
        path.lineTo(startPoint.x + delta1.x, startPoint.y + delta1.y);
        if (delta2 != null) {
            path.lineTo(startPoint.x + delta2.x, startPoint.y + delta2.y);
        }
        return path;
    }


    @Override
    public void leftSwipeAction() {

    }

    @Override
    public void rightSwipeAction() {

    }

}
